var query = require("../utils/connect");
var express = require('express');
var router = express.Router();
var { createToken, checkToken } = require("../utils/token")
var axios = require('axios')

router.get('/test', (req, res) => {
    query("select * from student", function (err, vals, fields) {
        //do something
        res.json(vals)
        if (err) {
            console.log(err);
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        } else {
            if (vals.length > 0) {
                console.log(1);
                console.log(vals[0]);
            } else {
                console.log(2);
            }
        }
    });
})


// 密码登录
router.post('/login', (req, res) => {
    var { role, name, password } = req.body;
    if (role * 1 < 1) {
        var sql = `select * from student where (name = '${name}' or phone='${name}') && password = '${password}'`;
    } else {
        var sql = `select * from teacher where (name = '${name}' or phone='${name}') && password = '${password}'`;
    }
    query(sql, function (err, vals, fields) {
        if (err) {
            console.log(err);
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        } else {
            if (vals.length == 1) {
                var token = createToken({
                    name: name,
                    phone: vals[0].phone,
                    role: vals[0].role,
                })
                res.json({
                    msg: "登录成功",
                    code: 200,
                    type: 1,
                    vals,
                    token
                })
            } else if (vals.length > 1) {
                res.json({
                    code: 402,
                    msg: "登录失败，存在同名，请用手机号登录！",
                    type: 0,
                })
            } else {
                res.json({
                    code: 403,
                    msg: "登录失败,手机号码或者密码不正确",
                    type: 0,
                })
            }
        }
    })
})

// 发送验证码
router.get('/sent', (req, res) => {
    var { phone, role } = req.query;
    if (role * 1 < 1) {
        var sql = `select * from student where phone = '${phone}'`;
    } else {
        var sql = `select * from teacher where phone = '${phone}'`;
    }
    query(sql, function (err, vals, fields) {
        if (err) {
            console.log(err);
            res.json({
                err,
                msg: "服务器异常- 未知错误",
                code: 500,
                type: 0
            })
        } else {
            if (vals.length > 0) {
                axios.get("http://47.104.209.44:3333/captcha/sent", {
                    params: {
                        phone: phone
                    }
                }).then(obj => {
                    if (obj.data.code == 200) {
                        res.json({
                            code: 200,   // 402  403 统一失败  
                            msg: "验证码发送成功",
                            type: 1,
                        })
                    } else {
                        res.json({
                            code: 402,
                            msg: "验证码发送失败-超过次数",
                            type: 0,
                        })
                    }
                })
            } else {
                res.json({
                    code: 402,
                    msg: "手机号码未注册",
                    type: 0,
                })
            }
        }
    })
})

// 校验验证码 
router.get('/verify', (req, res) => {
    var { phone, role, captcha } = req.query;
    if (role * 1 < 1) {
        var sql = `select * from student where phone = '${phone}'`;
    } else {
        var sql = `select * from teacher where phone = '${phone}'`;
    }
    query(sql, function (err, vals, fields) {
        if (err) {
            console.log(err);
            res.json({
                err,
                msg: "服务器异常- 未知错误",
                code: 500,
                type: 0
            })
        } else {
            if (vals.length > 0) {
                axios.get("http://47.104.209.44:3333/captcha/verify", {
                    params: {
                        phone,
                        captcha
                    }
                }).then(result => {
                    let { code } = result.data;
                    console.log(result.data)
                    if (code == 200) {
                        // token 

                        var token = createToken({
                            phone: phone,
                            captcha: captcha
                        })
                        res.json({
                            code: 200,
                            msg: "登录成功",
                            type: 1,
                            phone,
                            vals,
                            token
                        })
                    } else {
                        res.json({
                            code: 402,   // 402  403 统一失败  
                            msg: "校验失败,请重新登录",
                            type: 0,
                        })
                    }
                })
            } else {
                res.json({
                    code: 402,
                    msg: "手机号码未注册",
                    type: 0,
                })
            }
        }
    })
})

// 获取用户信息
router.get('/getuserinfo', (req, res) => {
    checkToken(req, res, ({ phone, role }) => {
        if (role * 1 < 1) {
            var sql = `select * from student where phone = '${phone}'`;
        } else {
            var sql = `select * from teacher where phone = '${phone}'`;
        }
        query(sql, function (err, vals, fields) {
            if (err) {
                console.log(err);
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "获取用户信息数据成功",
                    code: 200,
                    type: 1,
                    vals,
                })
            }
        })
    })
})

// 获取师生信息
router.get('/getuserlist', (req, res) => {
    var { role, phone, cid } = req.query;
    checkToken(req, res, () => {
        if (cid) {
            query(`select * from class c left join student s on c.cid = s.cid left join grade g on c.gid=g.gid where c.cid=${cid}`, function (err, vals, fields) {
                res.json({
                    msg: "获取学生信息数据成功",
                    code: 200,
                    type: 1,
                    vals,
                })
            })
        } else {
            if (role * 1 < 1) {
                query(`select cid from student where phone = '${phone}'`, function (err, vals, fields) {
                    query(`select * from class c left join student s on c.cid = s.cid left join grade g on c.gid=g.gid where c.cid=${vals[0].cid}`, function (err, vals, fields) {
                        res.json({
                            msg: "获取班级学生信息数据成功",
                            code: 200,
                            type: 1,
                            vals,
                        })
                    })
                })
            } else {
                query(`select * from teacher`, function (err, vals, fields) {
                    res.json({
                        msg: "获取教师信息数据成功",
                        code: 200,
                        type: 1,
                        vals,
                    })
                })
            }
        }
    })
})

//  获取年级列表
router.get('/getgrade', (req, res) => {
    checkToken(req, res, () => {
        query('select * from grade', function (err, vals, fields) {
            res.json({
                msg: "获取年级信息数据成功",
                code: 200,
                type: 1,
                vals,
            })
        })
    })
})

//  获取班级列表
router.get('/getclass', (req, res) => {
    var { gid } = req.query;
    checkToken(req, res, () => {
        query(`select * from class where gid = ${gid}`, function (err, vals, fields) {
            res.json({
                msg: "获取班级信息数据成功",
                code: 200,
                type: 1,
                vals,
            })
        })
    })
})

// 获取学号或工号
router.get('/getid', (req, res) => {
    var { role, grade } = req.query;
    checkToken(req, res, () => {
        if (role * 1 > 0) {
            var year = '' + new Date().getFullYear();
            year = year.substring(2)
            var month = new Date().getMonth() + 1;
            if (month < 9) {
                year = year - 1
            }
            month = '' + month > 9 ? month : '0' + month;
            var str = year + month;
            var sql = `select tid from teacher where tid like '${str}%' order by tid`
        } else {
            var str = grade;
            var sql = `select sid from student where sid like '${str}%' order by sid`
        }
        query(sql, function (err, vals, fields) {
            if (vals.length > 0) {
                res.json({
                    code: 233,
                    type: 1,
                    vals,
                })
            } else {
                res.json({
                    str,
                    code: 233,
                    type: 0
                })
            }
        })
    })
})

// 添加教师或学生
router.post('/adduser', (req, res) => {
    var { role, name, age, sex, phone, password, id } = req.body;
    checkToken(req, res, () => {
        var sql = `select * from teacher t,student s where t.phone='${phone}' or s.phone='${phone}'`
        query(sql, function (err, vals, fields) {
            if (vals.length > 0) {
                res.json({
                    msg: "手机号已存在，添加失败！",
                    code: 402,
                    type: 0,
                })
            } else {
                if (role * 1 > 0) {
                    var sql = `insert teacher values(${id},'${name}',${age},${sex},'${phone}','${password}',${role},null)`
                } else {
                    var { cid } = req.body;
                    var sql = `insert student values(${id},'${name}',${age},${sex},'${phone}','${password}',${cid},${role},null,null)`
                }
                query(sql, function (err, vals, fields) {
                    if (err) {
                        console.log(err);
                        res.json({
                            err,
                            msg: "服务器异常- 未知错误",
                            code: 500,
                            type: 0
                        })
                    } else {
                        res.json({
                            msg: "添加成功",
                            code: 200,
                            type: 1,
                        })
                    }
                })
            }
        })
    })
})


// 获取男女占比
router.get('/getsex', (req, res) => {
    var { gid } = req.query;
    var result = [];
    checkToken(req, res, () => {
        if (!gid) {
            var sql = `select g.gname,count(s.sid) count,s.sex from student s,grade g,class c where g.gid = c.gid and c.cid = s.cid GROUP BY g.gname,s.sex`
            query('SELECT count(tid) count from teacher GROUP BY sex', function (err, vals, fields) {
                result = vals;
                query(sql, function (err, vals, fields) {
                    if (err) {
                        res.json({
                            msg: "服务器异常- 未知错误",
                            code: 500,
                            type: 0
                        })
                    } else {
                        res.json({
                            msg: "学生人数查询成功",
                            code: 200,
                            type: 1,
                            vals,
                            result
                        })
                    }
                })
            })
        } else {
            var sql = `select g.gname,c.cname,count(s.sid) count from grade g,class c,student s where g.gid = c.gid and c.cid = s.cid and g.gid = ${gid} GROUP BY g.gname,c.cname`
            query(sql, function (err, vals, fields) {
                if (err) {
                    res.json({
                        msg: "服务器异常- 未知错误",
                        code: 500,
                        type: 0
                    })
                } else {
                    res.json({
                        msg: "学生人数查询成功",
                        code: 200,
                        type: 1,
                        vals,
                        result
                    })
                }
            })
        }
    })
})

// 获取课程表
router.get('/getcourse', (req, res) => {
    var { seid, gname } = req.query;
    checkToken(req, res, ({ role }) => {
        query('update course set mon = null,tue = null,wed = null,thu = null,fri = null', function (err, value, fields) {
            var arr = [];
            var str = 0;
            if (role * 1 > 0) {
                query(`select seid from selects where gname = '${gname}'`, function (err, vals, fields) {
                    if (vals.length > 0) {
                        vals.forEach(item => arr.push(item.seid))
                        if (arr.length == vals.length) {
                            arr.forEach(item => {
                                query(`select * from selects where seid = ${item}`, function (err, result, fields) {
                                    query(`update course set ${result[0].week} = '${result[0].sename}-${result[0].tname}' where coid = ${result[0].time}`, function (err, val, fields) {
                                        if (val.changedRows > 0) {
                                            str += 1;
                                            if (str == arr.length) {
                                                query('select * from course', function (err, vals, fields) {
                                                    if (!err) {
                                                        res.json({
                                                            msg: "获取课程表成功",
                                                            code: 200,
                                                            type: 1,
                                                            vals,
                                                        })
                                                    } else {
                                                        res.json({
                                                            msg: "服务器异常- 未知错误",
                                                            code: 500,
                                                            type: 0
                                                        })
                                                    }
                                                })
                                            }
                                        }
                                    })
                                })
                            })
                        }
                    } else {
                        query('select * from course', function (err, vals, fields) {
                            if (!err) {
                                res.json({
                                    msg: "获取课程表成功",
                                    code: 200,
                                    type: 1,
                                    vals,
                                })
                            } else {
                                res.json({
                                    msg: "服务器异常- 未知错误",
                                    code: 500,
                                    type: 0
                                })
                            }
                        })
                    }
                })
            } else {
                if (seid) {
                    arr = seid.split(',');
                    arr.forEach(item => {
                        query(`select * from selects where seid = ${item}`, function (err, result, fields) {
                            query(`update course set ${result[0].week} = '${result[0].sename}-${result[0].tname}' where coid = ${result[0].time}`, function (err, val, fields) {
                                if (val.changedRows > 0) {
                                    str += 1;
                                    if (str == arr.length) {
                                        query('select * from course', function (err, vals, fields) {
                                            if (!err) {
                                                res.json({
                                                    msg: "获取课程表成功",
                                                    code: 200,
                                                    type: 1,
                                                    vals,
                                                })
                                            } else {
                                                res.json({
                                                    msg: "服务器异常- 未知错误",
                                                    code: 500,
                                                    type: 0
                                                })
                                            }
                                        })
                                    }
                                }
                            })
                        })
                    })
                } else {
                    query('select * from course', function (err, vals, fields) {
                        if (!err) {
                            res.json({
                                msg: "获取课程表成功",
                                code: 200,
                                type: 1,
                                vals,
                            })
                        } else {
                            res.json({
                                msg: "服务器异常- 未知错误",
                                code: 500,
                                type: 0
                            })
                        }
                    })
                }
            }
        })
    })
})


// 发布选课
router.post('/addselect', (req, res) => {
    var { sename, tname, week, describe, count, score, gname } = req.body;
    checkToken(req, res, () => {
        var sqls = `select * from selects where week = '${week[0]}' and time = ${week[1]} and ((gname = '${gname}' and tname = '${tname}') or tname = '${tname}')`
        query(sqls, function (err, result, fields) {
            if (result.length > 0) {
                res.json({
                    msg: "该年级或老师此时间段已存在课程",
                    code: 402,
                    type: 0,
                })
            } else {
                var sql = `insert selects values(null,'${sename}','${tname}','${week[0]}',${week[1]},'${describe}',${count},${score},'${gname}',0)`
                query(sql, function (err, vals, fields) {
                    if (err) {
                        res.json({
                            err,
                            msg: "服务器异常- 未知错误",
                            code: 500,
                            type: 0
                        })
                    } else {
                        res.json({
                            msg: "发布选课成功",
                            code: 200,
                            type: 1,
                        })
                    }
                })
            }
        })
    })
})

// 获取教师
router.get('/getteacher', (req, res) => {
    checkToken(req, res, () => {
        var sql = `select * from teacher`
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "获取教师信息成功",
                    code: 200,
                    type: 1,
                    vals
                })
            }
        })
    })
})

//  获取选课表
router.get('/getselects', (req, res) => {
    checkToken(req, res, () => {
        var sql = `select * from selects`
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "获取选课表成功",
                    code: 200,
                    type: 1,
                    vals
                })
            }
        })
    })
})

//  添加选课
router.post('/addmyselect', (req, res) => {
    var { seid, sid } = req.body;
    checkToken(req, res, () => {
        var sql = `update student set seid='${seid}' where sid = ${sid}`
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                var arr = seid.split(',');
                var str = 0;
                arr.forEach(item => {
                    query(`update selects a INNER JOIN (select count,scount from selects where seid = ${item}) b set a.count = b.count - 1,a.scount = b.scount + 1 where a.seid = ${item}`, function (err, vals, fields) {
                        if (vals.changedRows > 0) {
                            str += 1;
                            if (str == arr.length) {
                                res.json({
                                    msg: "添加选课成功",
                                    code: 200,
                                    type: 1,
                                    seid
                                })
                            } else {
                                res.json({
                                    msg: "服务器异常- 未知错误",
                                    code: 500,
                                    type: 0
                                })
                            }
                        }
                    })
                })
            }
        })
    })
})

// 搜索选课
router.get('/searchselects', (req, res) => {
    var { keywords } = req.query;
    checkToken(req, res, () => {
        var sql = `select * from selects where sename like '%${keywords}%' or tname like '%${keywords}%' or gname like '%${keywords}%'`
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "搜索成功",
                    code: 200,
                    type: 1,
                    vals
                })
            }
        })
    })
})

// 上传头像  
// multer 
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function (req, file, cb) {   // 头像存放的目录 
        cb(null, './public/avatar')    // . 文件路径
    },
    filename: function (req, file, cb) {  // 新的文件名 
        cb(null, Date.now() + "WH2008" + file.originalname);  //  originalname 原文件名  1.jpg    145123213123WH20081.jpg
    }
})


var upload = multer({ storage: storage }).any();  // 可以上传任何格式的图片

// 上传图片
router.post('/uploadanyimg', upload, (req, res) => {
    checkToken(req, res, ({ phone }) => {
        if (req.files) {
            var path = req.files[0].path;
            res.json({
                code: 200,
                msg: "上传成功",
                path,
                type: 1,
            })
        } else {
            res.json({
                code: 500,   // 500 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        }
    })
})

// 上传头像
router.post('/uploadavatar', upload, (req, res) => {
    checkToken(req, res, ({ phone, role }) => {
        if (req.files) {
            var path = req.files[0].path;
            path = path.replace(/\\/g, '/')
            console.log(path);

            if (role * 1 > 0) {
                var sql = `update teacher set avatar = '${path}' where phone = '${phone}'`
            } else {
                var sql = `update student set avatar = '${path}' where phone = '${phone}'`
            }
            query(sql, function (err, vals, fields) {
                if (err) {
                    res.json({
                        code: 500,   // 500 统一失败  
                        msg: "服务器异常- 未知错误",
                        type: 0,
                    })
                } else {
                    res.json({
                        code: 200,
                        msg: "上传成功",
                        type: 1,
                        path
                    })
                }
            })
        }
    })
})


//  添加公告
router.post('/addnotice', (req, res) => {
    var { title, content, img, author } = req.body;
    checkToken(req, res, () => {
        if (img) {
            img = img.replace(/\\/g, '/')
            var sql = `insert notice value(null,'${title}','${content}','${img}',NOW(),'${author}')`
        } else {
            var sql = `insert notice value(null,'${title}','${content}',null,NOW(),'${author}')`
        }
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "发布公告成功",
                    code: 200,
                    type: 1,
                })
            }
        })
    })
})

// 获取公告列表
router.get('/getnotice', (req, res) => {
    checkToken(req, res, () => {
        var sql = `select * from notice`;
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "获取公告列表成功",
                    code: 200,
                    type: 1,
                    vals
                })
            }
        })
    })
})

// 删除一条公告
router.get('/deletenotice', (req, res) => {
    var { nid } = req.query
    checkToken(req, res, () => {
        query(`delete from notice where nid = ${nid}`, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "删除成功",
                    code: 200,
                    type: 1,
                })
            }
        })
    })
})

// 搜索公告
router.get('/searchnotice', (req, res) => {
    var { keywords } = req.query;
    checkToken(req, res, () => {
        var sql = `select * from notice where title like '%${keywords}%' or content like '%${keywords}%' or author like '%${keywords}%'`
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "搜索成功",
                    code: 200,
                    type: 1,
                    vals
                })
            }
        })
    })
})

//  更新公告
router.post('/updatenotice', (req, res) => {
    var { title, content, img, author } = req.body;
    checkToken(req, res, () => {
        img = img.replace(/\\/g, '/')
        var sql = `update notice set title = '${title}',content = '${content}',img = '${img}',time = NOW(),author = '${author}'`
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "修改成功",
                    code: 200,
                    type: 1,
                })
            }
        })
    })
})

//  添加建议
router.post('/addadvice', (req, res) => {
    var { title, content, category, name } = req.body;
    checkToken(req, res, () => {
        var sql = `insert advice value(null,'${title}','${content}','${category}',NOW(),'${name}')`
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "发布建议成功",
                    code: 200,
                    type: 1,
                })
            }
        })
    })
})

// 获取建议列表
router.get('/getadvice', (req, res) => {
    checkToken(req, res, () => {
        var sql = `select * from advice`;
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "获取建议列表成功",
                    code: 200,
                    type: 1,
                    vals
                })
            }
        })
    })
})

// 搜索建议
router.get('/searchadvice', (req, res) => {
    var { keywords } = req.query;
    checkToken(req, res, () => {
        var sql = `select * from advice where title like '%${keywords}%' or content like '%${keywords}%' or name like '%${keywords}%'`
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "搜索成功",
                    code: 200,
                    type: 1,
                    vals
                })
            }
        })
    })
})

// 获取个人信息
router.get('/getmine', (req, res) => {
    var { sid, role, tid } = req.query;
    checkToken(req, res, () => {
        if (role * 1 > 0) {
            var sql = `select * from teacher where tid = ${tid}`
        } else {
            var sql = `select * from class c left join student s on c.cid = s.cid left join grade g on c.gid=g.gid where s.sid=${sid}`
        }
        query(sql, function (err, vals, fields) {
            if (err) {
                res.json({
                    err,
                    msg: "服务器异常- 未知错误",
                    code: 500,
                    type: 0
                })
            } else {
                res.json({
                    msg: "获取个人信息成功",
                    code: 200,
                    type: 1,
                    vals,
                })
            }
        })
    })
})

// 修改密码
router.post('/updatepwd', (req, res) => {
    var { oldpassword, newpassword } = req.body
    checkToken(req, res, ({ phone, role }) => {
        if (role * 1 > 0) {
            var sql = `select * from teacher where phone = '${phone}' and password = '${oldpassword}'`
            var sql2 = `update teacher set password = '${newpassword}' where phone = '${phone}'`
        } else {
            var sql = `select * from student where phone = '${phone}' and password = '${oldpassword}'`
            var sql2 = `update student set password = '${newpassword}' where phone = '${phone}'`
        }
        query(sql, function (err, vals, fields) {
            if (vals.length > 0) {
                query(sql2, function (err, val, fields) {
                    if (err) {
                        res.json({
                            err,
                            msg: "服务器异常- 未知错误",
                            code: 500,
                            type: 0
                        })
                    } else {
                        res.json({
                            msg: "修改密码成功,请重新登录",
                            code: 200,
                            type: 1,
                        })
                    }
                })
            } else {
                res.json({
                    err,
                    msg: "原密码不正确",
                    code: 402,
                    type: 0
                })
            }
        })
    })
})

// 首页信息
router.get('/gethome', (req, res) => {
    var { cid } = req.query
    checkToken(req, res, ({ role }) => {
        if (cid) {
            var sql = `select count(*) pcount from class c left join student s on c.cid = s.cid left join grade g on c.gid=g.gid where c.cid=${cid}`
        } else {
            var sql = `select count(s.sid)+t.total pcount from student s,(select count(tid) total from teacher) t`
        }
        query(sql, function (err, vals, fields) {
            var pcount = vals[0].pcount;
            query('select count(*) scount,n.total ncount from selects,(select count(*) total from notice) n', function (err, val, fields) {
                var obj = val[0]
                obj.pcount = pcount
                query('select * from notice', function (err, value, fields) {
                    query('select gname,sum(scount) scount,sum(count) count from selects group by gname', function (err, result, fields) {
                        if (err) {
                            res.json({
                                err,
                                msg: "服务器异常- 未知错误",
                                code: 500,
                                type: 0
                            })
                        } else {
                            res.json({
                                msg: "获取首页信息成功",
                                code: 200,
                                type: 1,
                                obj,
                                value,
                                result,
                            })
                        }
                    })
                })
            })
        })
    })
})


module.exports = router;
